#########################################
# Setup
#########################################

rm(list = ls())
setwd(".")

library(tidyverse)
library(modelsummary)
library(ggpubr)

#########################################
# Load and Clean Data
#########################################

survey <- read.csv("survey_data.csv") 
survey <-  survey %>% filter(mvc_correct == 2)
survey_inc <- survey %>% filter(condition %in% c(1, 2, 3, 4)) %>% filter(mvc_correct == 2)
survey_opp <- survey %>% filter(condition %in% c(5, 6, 7, 8)) %>% filter(mvc_correct == 2)


survey_inc <- survey_inc %>%
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief))

survey_opp<- survey_opp %>%
  mutate(prop_belief = case_when(is.na(prop_belief) == T ~ 50,
                                 .default = prop_belief),
         opp_belief = case_when(is.na(opp_belief) == T ~ 80,
                                .default = opp_belief))

#########################################
# Plot Opposition Veto Beliefs
#########################################


# All 


plot.new()
p1 <- ggplot(data = survey %>% filter(condition %in% c(2, 4, 6, 8)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey$opp_belief[survey$condition %in% c(2, 4, 6, 8)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Total Sample / State Quo Condition)")+
  ylim(0,0.15)

p1
#ggsave("veto_beliefs_total_sample_SQ.png", p1, width = 10, height = 5)



# Incumbent Aligned Sample

plot.new()
p2 <- ggplot(data = survey_inc %>% filter(condition %in% c(2,4)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept = mean(survey_inc$opp_belief[survey_inc$condition %in% c(2,4)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Incumbent Aligned Sample / Status Quo Condition)")+
  ylim(0,0.15)

p2
#ggsave("veto_beliefs_incumbentaligned_sample_SQ.png", p2, width = 10, height = 5)

# Opposition Aligned Sample

plot.new()
p3 <- ggplot(data = survey_opp %>% filter(condition %in% c(6,8)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey_opp$opp_belief[survey_opp$condition %in% c(6,8)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Opposition Aligned Sample / Status Quo Condition)")+
  ylim(0,0.15)

p3
#ggsave("veto_beliefs_oppositionaligned_sample_SQ.png", p3, width = 10, height = 5)


# Incumbent Aligned by Identity 

plot.new()
p4 <- ggplot(data = survey_inc %>% filter(condition %in% c(2)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey_inc$opp_belief[survey_inc$condition %in% c(2)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Incumbent Aligned Sample / SQ Condition / No identity)")+
  ylim(0,0.15)

p4
#ggsave("veto_beliefs_incumbentaligned_sample_noidentity_SQ.png", p4, width = 10, height = 5)

plot.new()
p5 <- ggplot(data = survey_inc %>% filter(condition %in% c(4)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey_inc$opp_belief[survey_inc$condition %in% c(4)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Incumbent Aligned Sample / SQ Condition / Incumbent Aligned)")+
  ylim(0,0.15)

p5
#ggsave("veto_beliefs_incumbentaligned_sample_incumbentaligned_SQ.png", p5, width = 10, height = 5)


# Opposition Aligned by Incumbent

plot.new()
p6 <- ggplot(data = survey_opp %>% filter(condition %in% c(6)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey_opp$opp_belief[survey_opp$condition %in% c(6)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power(Opposition Aligned Sample / Status Quo Condition / No Identity)")+
  ylim(0,0.15)

p6
#ggsave("veto_beliefs_oppositionaligned_sample_noidentity_SQ.png", p6, width = 10, height = 5)

plot.new()
p7 <- ggplot(data = survey_opp %>% filter(condition %in% c(8)), aes(x = opp_belief)) +
  geom_histogram(aes(y = ..density.., color = condition), bins = 30, fill = "lightblue", color = "black") +
  geom_density(color = "red", size = 1) + 
  theme_bw()+
  geom_vline(aes(xintercept =mean(survey_opp$opp_belief[survey_opp$condition %in% c(8)] , na.rm = TRUE)
  ),linetype = "dashed")+
  ggtitle("Density for Beliefs on Veto Power (Opposition Aligned Sample / Status Quo Condition / Opposition Aligned Identity)")+
  ylim(0,0.15)


p7
#ggsave("veto_beliefs_oppositionaligned_sample_oppositionaligned_SQ.png", p7, width = 10, height = 5)

